The .substring() method returns a portion of a string.
public static int loopIt(String str) {
int result = 0;
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).equals("a"))
result++;
}
return result;
}
| Function Call | Return Value | |||
|---|---|---|---|---|
| loopIt("athenian") | → | |||
| loopIt("apples") | → | |||
| loopIt("hello") | → | |||
| loopIt("") | → | |||
| loopIt(" ") | → | |||
| loopIt("alphabet") | → | |||
Experiment with this code on Gitpod.io